package com.yc.login.web;

import com.yc.common.biz.BizException;
import com.yc.common.vo.Result;
import com.yc.login.biz.LoginBiz;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginAction {

    @Resource
    private LoginBiz loginBiz;

    @RequestMapping("login")
    public Result login(String username, String password, HttpSession session) throws BizException, IllegalAccessException {
        Object login = loginBiz.login(username, password);
        session.setAttribute("myinfo",login);
        Object backURI = session.getAttribute("backURI");
        if (backURI != null){
            session.removeAttribute("backURI");
            Map<String,Object> map = new HashMap<>();
            for (Field field : login.getClass().getDeclaredFields()){
                field.setAccessible(true);
                Object value = field.get(login);
                String name = field.getName();
                map.put(name, value);
            }
            map.put("backURI", backURI);
            login = map;
        }
        return Result.success("登录成功",login);
    }

    @RequestMapping("myinfo")
    public Result myinfo(@SessionAttribute(required = false) Object myinfo){
     //   Result ret;
        if (myinfo == null){
            return Result.failure("未登录");
        }else {
            return Result.success("已登录",myinfo);
        }
    }

    @RequestMapping("logout")
    public Result logout(HttpSession session){
        session.invalidate();
        return Result.success("退出成功");
    }

    @ExceptionHandler(BizException.class)
    public Result handlerBizException(BizException e){
        e.printStackTrace();
        return Result.failure(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handlerException(BizException e){
        e.printStackTrace();
        return Result.failure("业务繁忙请稍后在试");
    }

    @RequestMapping("modifyPwd")
    public Result modifyPwd(String password,String newPassword, @SessionAttribute Object myinfo) throws BizException {
        loginBiz.modifyPwd(myinfo,password,newPassword);
        return Result.success("密码修改成功");
    }

    @RequestMapping("forgetPwd")
    public Result forgetPwd(String username , HttpSession session  ) throws BizException {
        String vcode = loginBiz.sendPwdEmail(username);
        session.setAttribute("scode",vcode);
        session.setAttribute("username", username);
        return Result.success("验证码证件已经发送到您的邮箱,请查收!");
    }

    @RequestMapping("modifyPwd1")
    public Result modifyPwd(String newPassword, String vcode,
                            @SessionAttribute String scode,
                            @SessionAttribute String username) throws BizException {
        loginBiz.modifyPwd(username,newPassword,vcode,scode);
        return Result.success("密码重置成功！");
    }

}
